﻿using FileZillaConfig.ConfigClasses;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace FileZillaConfig
{
    public class FileZillaManager : FileZillaXML
    {
        private string _serverPath;
        

        #region PROPERTIES
        /// <summary>
        /// Global server settings
        /// </summary>
        public ServerSettings Settings
        {
            get
            {
                return this._settings;
            }
        }

        /// <summary>
        /// Server groups
        /// </summary>
        public List<Group> Groups
        {
            get
            {
                return this._groups;
            }
            set
            {
                this._groups = value;
            }
        }

        /// <summary>
        /// Server users
        /// </summary>
        public List<User> Users
        {
            get
            {
                return this._users;
            }
            set
            {
                this._users = value;
            }
        }

        #endregion

        /// <summary>
        /// Create new FileZilla Server configuration object
        /// </summary>
        /// <param name="FileZillaDir">Directory where FileZilla Server is installed</param>
        public FileZillaManager(string FileZillaDir = @"C:\Program Files (x86)\FileZilla Server\")
        {
            Char[] slash = new Char[1];
            slash[0] = '\\';

            this._serverPath = FileZillaDir.TrimEnd(slash);

            //Construct config classes
            this._settings = new ServerSettings();
            this._groups = new List<Group>();
            this._users = new List<User>();
        }                                            

        public void LoadConfig()
        {
            XDocument _configXML = new XDocument();

            if(File.Exists(_serverPath + @"\FileZilla Server.xml"))
            {
                _configXML = new XDocument();
                _configXML = XDocument.Load(_serverPath + @"\FileZilla Server.xml");
                this._setConfigFromXML(_configXML);
            } else 
            {
                throw new System.Exception("Config file doesn't exist in specified directory");
            }
        }

        /// <summary>
        /// Save all changes to the config file
        /// </summary>
        public void Save()
        {
            XDocument xdoc = this._getXML();
            xdoc.Save(_serverPath + @"\FileZilla Server.xml");
        }

        /// <summary>
        /// Reload server configuration
        /// </summary>
        public void Reload()
        {
            System.Diagnostics.Process.Start(_serverPath + @"\FileZilla Server.exe", "/reload-config");    
        }

        /// <summary>
        /// Save all changes and reload server configuration
        /// </summary>
        public void SaveAndReload()
        {
            Save();
            Reload();
        }

        /// <summary>
        /// Generate XML document with current settings
        /// </summary>
        public XDocument GenerateXML()
        {
            return this._getXML();
        }
    }
}
